﻿using System;
using System.Collections.Generic;
using UtfprLibrary.Communication.Contracts;
using UtfprLibrary.Exceptions;

namespace UtfprLibrary.Communication {
    /// <summary>
    /// TODO
    /// </summary>
    public class Filter {
        static Dictionary<string, List<IPublishing>> _subscribersList = new Dictionary<string, List<IPublishing>>();

        /// <summary>
        /// TODO
        /// </summary>
        static public Dictionary<string, List<IPublishing>> SubscribersList {
            get {
                try {
                    lock (typeof(Filter)) {
                        return _subscribersList;
                    }
                } catch (Exception ex) {
                    throw new UtfprCommunicationException(Properties.Resources.Communication_Filter_ExMsg_SubscribersList, ex);
                }
            }

        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="subject"></param>
        /// <returns></returns>
        static public List<IPublishing> GetSubscribers(String subject) {
            try {
                lock (typeof(Filter)) {
                    if (SubscribersList.ContainsKey(subject)) {
                        return SubscribersList[subject];
                    } else {
                        return null;
                    }
                }
            } catch (Exception ex) {
                throw new UtfprCommunicationException(Properties.Resources.Communication_Filter_ExMsg_GetSubscribers, ex);
            }
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="subscriberCallbackReference"></param>
        static public void AddSubscriber(String subject, IPublishing subscriberCallbackReference) {
            try {
                lock (typeof(Filter)) {
                    if (SubscribersList.ContainsKey(subject)) {
                        if (!SubscribersList[subject].Contains(subscriberCallbackReference)) {
                            SubscribersList[subject].Add(subscriberCallbackReference);
                        }
                    } else {
                        List<IPublishing> newSubscribersList = new List<IPublishing>();
                        newSubscribersList.Add(subscriberCallbackReference);
                        SubscribersList.Add(subject, newSubscribersList);
                    }
                }
            } catch (Exception ex) {
                throw new UtfprCommunicationException(Properties.Resources.Communication_Filter_ExMsg_AddSubscriber, ex);
            }
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="subscriberCallbackReference"></param>
        static public void RemoveSubscriber(String subject, IPublishing subscriberCallbackReference) {
            try {
                lock (typeof(Filter)) {
                    if (SubscribersList.ContainsKey(subject)) {
                        if (SubscribersList[subject].Contains(subscriberCallbackReference)) {
                            SubscribersList[subject].Remove(subscriberCallbackReference);
                        }
                    }
                }
            } catch (Exception ex) {
                throw new UtfprCommunicationException(Properties.Resources.Communication_Filter_ExMsg_RemoveSubscriber, ex);
            }
        }
    }
}
